//
//  ZYHouseItemCellView.m
//  YiLin
//
//  Created by Zyark on 2018/6/1.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYHouseItemCellView.h"
#import "ZYHouse.h"

@interface ZYHouseItemCellView()
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UIImageView *houseImageView;
@property(nonatomic,strong) UILabel *nameLabel;
@property(nonatomic,strong) UILabel *addressLabel;
@property(nonatomic,strong) UILabel *statusLabel;
@property(nonatomic,strong) UIButton *editBtn;
@property(nonatomic,strong) UIButton *deleteBtn;
@end

@implementation ZYHouseItemCellView

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setupView];
    }
    return self;
}

-(void)setupView{
    UIView *bgView = [[UIView alloc] init];
    UIImageView *houseImageView = [[UIImageView alloc] init];
    UILabel *nameLabel = [[UILabel alloc] init];
    UILabel *addressLabel = [[UILabel alloc] init];
    UILabel *statusLabel = [[UILabel alloc] init];
    UIButton *editBtn = [[UIButton alloc] init];
    UIButton *deleteBtn = [[UIButton alloc] init];
    
    [self addSubview:bgView];
    [bgView addSubview:houseImageView];
    [bgView addSubview:nameLabel];
    [bgView addSubview:addressLabel];
    [bgView addSubview:statusLabel];
    [bgView addSubview:editBtn];
    [bgView addSubview:deleteBtn];
    
    //约束
//    ZYWeakSelf(weakSelf)
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-20);
    }];
//    CGFloat width = self.height - 20 * 2;
    [houseImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(15);
        make.top.equalTo(bgView).offset(20);
//        make.bottom.equalTo(bgView).offset(20);
//        make.width.mas_equalTo(make.height);
        make.width.equalTo(bgView.mas_height).offset(-20*2);
        make.height.equalTo(bgView.mas_height).offset(-20*2);
    }];
//
    [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(20);
        make.top.equalTo(houseImageView.mas_top);
        make.trailing.equalTo(bgView.mas_trailing).offset(-15);
    }];

    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(houseImageView.mas_trailing).offset(8);
        make.top.equalTo(houseImageView);
        make.trailing.equalTo(editBtn.mas_leading).offset(-5);
        make.height.equalTo(editBtn.mas_height).multipliedBy(2);
    }];

    [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(nameLabel.mas_leading);
        make.trailing.equalTo(editBtn.mas_trailing);
        make.bottom.equalTo(houseImageView.mas_bottom);
        make.height.equalTo(editBtn.mas_height);
    }];
    
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(statusLabel.mas_centerY);
        make.trailing.equalTo(bgView.mas_trailing).offset(-15);
    }];

    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(nameLabel.mas_leading);
        make.trailing.equalTo(editBtn.mas_trailing);
        make.bottom.equalTo(statusLabel.mas_top).offset(-10);
    }];
    
    nameLabel.textColor = kLabelColor;
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.font = [UIFont systemFontOfSize:14];
    nameLabel.numberOfLines = 2;
    
    addressLabel.textColor = RGBA(102, 102, 102, 1);
    addressLabel.textAlignment = NSTextAlignmentLeft;
    addressLabel.font = [UIFont systemFontOfSize:12];
    addressLabel.numberOfLines = 2;
    
    statusLabel.textColor = RGBA(102, 102, 102, 1);
    statusLabel.textAlignment = NSTextAlignmentLeft;
    statusLabel.font = [UIFont systemFontOfSize:12];
    
    [editBtn setTitleColor:kMainColor forState:UIControlStateNormal];
    [editBtn setBackgroundColor:[UIColor whiteColor]];
    [editBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    editBtn.layer.borderWidth = 1;
    editBtn.layer.borderColor = kMainColor.CGColor;
    editBtn.layer.cornerRadius = 2;
    editBtn.layer.masksToBounds = YES;
    [editBtn addTarget:self action:@selector(doEdit) forControlEvents:UIControlEventTouchUpInside];
    [editBtn setTitle:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_HOUSE_LIST_EDIT_BTN_TITLE] forState:UIControlStateNormal];
    
    UIColor *redColor = [UIColor redColor];
    [deleteBtn setTitleColor:redColor forState:UIControlStateNormal];
    [deleteBtn setBackgroundColor:[UIColor whiteColor]];
    [deleteBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    deleteBtn.layer.borderWidth = 1;
    deleteBtn.layer.borderColor = redColor.CGColor;
    deleteBtn.layer.cornerRadius = 2;
    deleteBtn.layer.masksToBounds = YES;
    [deleteBtn addTarget:self action:@selector(doDelete) forControlEvents:UIControlEventTouchUpInside];
    [deleteBtn setTitle:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_DELETE_BUTTON_TITLE] forState:UIControlStateNormal];
    
    houseImageView.layer.cornerRadius = 5;
    houseImageView.layer.masksToBounds = YES;
    
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    bgView.layer.borderWidth = 2;
    bgView.layer.borderColor = RGB(245, 248, 249).CGColor;
//    bgView.layer.shadowColor = [UIColor blackColor].CGColor;//设置阴影的颜色
//    bgView.layer.shadowOpacity = 0.8;//设置阴影的透明度
//    bgView.layer.shadowOffset = CGSizeMake(1, 1);//设置阴影的偏移量
//    bgView.layer.shadowRadius = 3;//设置阴影的圆角

    _bgView = bgView;
    _houseImageView = houseImageView;
    _nameLabel = nameLabel;
    _addressLabel = addressLabel;
    _statusLabel = statusLabel;
    _editBtn = editBtn;
    _deleteBtn = deleteBtn;
}

-(void)doEdit{
    if([_delegate respondsToSelector:@selector(houseItemCellView:doEdit:)]){
        [_delegate houseItemCellView:self doEdit:_house];
    }
}

-(void)doDelete{
    if([_delegate respondsToSelector:@selector(houseItemCellView:doDelete:)]){
        [_delegate houseItemCellView:self doDelete:_house];
    }
}

-(void)setHouse:(ZYHouse *)house{
    _house = house;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    [_houseImageView setImageURL:[NSURL URLWithString:house.houseImageUrl]];
    _nameLabel.text = [NSString stringWithFormat:@"%@:%@",[language getStringByKey:LC_ME_MAIN_HOUSE_LIST_NAME_LABEL],house.name];
    _addressLabel.text = [NSString stringWithFormat:@"%@:%@",[language getStringByKey:LC_ME_MAIN_HOUSE_LIST_ADDRESS_LABEL],house.address];
    _statusLabel.text = [NSString stringWithFormat:@"%@:%@",[language getStringByKey:LC_ME_MAIN_HOUSE_LIST_STATUS_LABEL],house.statusStr];
}
@end
